package com.spider.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.spider.domain.Movie;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.*;

public class SpiderYangGuang {

    public static List<Object> test() {
        String uri = "https://www.ygdy8.com/html/gndy/dyzz/list_23_1.html";
        //String html = sendUri(uri);
        //List<Object> li = getContent(html);
        List<Object> list = new ArrayList<>();
        /*for(Object obj : li){
            //String nodeHtml = sendUri(obj+"");
            //getNodeContent(nodeHtml,list);
        }*/
        return list;
    }


    public static String sendUri(String uri,int socket,int connect,String encoding,String method){
        List<Object> list = new ArrayList<>();
        //生成一个httpclient,相当于打开一个浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //设置访问超时时间
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(socket).setConnectTimeout(connect).build();
        CloseableHttpResponse response = null;
        String html = null;
        HttpGet request = new HttpGet(uri);
        try{

            //HttpPost request = new HttpPost(uri);
            request.setConfig(requestConfig);
            //执行get请求，相当于输入地址，回车访问
            response = httpClient.execute(request);

            //判断访问状态码 200为成功，404为访问失败
            if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
                //获取响应内容
                HttpEntity httpEntity = response.getEntity();
                html = EntityUtils.toString(httpEntity,encoding);

            }else{
                System.out.println("页面访问失败，错误代码:"+response.getStatusLine().getStatusCode());
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //关闭
            HttpClientUtils.closeQuietly(response);
            HttpClientUtils.closeQuietly(httpClient);
        }

        return html;
    }

    public static void getContent(Map<String,Object> map, JSONArray vName, JSONArray vValue){//a1
        Object obj = map.get("html");

        if(obj instanceof Collection){
            List<String> list = (List<String>)obj;
            for(String html:list){
                getNodeContent(html,map,vName,vValue);
            }
        }else {
            String html = obj.toString();
            getNodeContent(html,map,vName,vValue);
        }

        System.out.println("完成----");

    }
    public static void getNodeContent(String html,Map<String,Object> map, JSONArray vName, JSONArray vValue){
        //把字符串转换为doc对象
        Document document = Jsoup.parse(html);
        String rootPath = "";
        for(int i=0;i<vValue.size();i++){

            JSONObject jo = (JSONObject) JSON.parse(vValue.getString(i));
            if(jo.containsKey("selects")){
                String cssQuery = jo.getString("selects");
                Elements elements = document.select(cssQuery);
                map.put(vName.getString(i),elements);
                i++;
                JSONObject jnode = (JSONObject) JSON.parse(vValue.getString(i));
                if(jnode.containsKey("select")){
                    String str = jnode.getString("select");
                    List<Map<String,String>> ms = new ArrayList<>();
                    //判断是否已经存在在map
                    if(map.containsKey(vName.getString(i))) {
                        ms = (List<Map<String,String>>)map.get(vName.getString(i));
                    }
                    for(Element element:elements){
                        //tr[1]/td[1]/a[0]/text(),@href
                        String strs[] = str.split("/");
                        for(String s:strs) {
                            String regex = "[a-z]+\\[\\d+\\]";
                            if(s.matches(regex)) {
                                //tr[1]
                                String ss[] = s.split("\\[");
                                cssQuery = ss[0];
                                int number = Integer.parseInt(ss[1].substring(0, ss[1].length() - 1));
                                element = element.select(cssQuery).get(number);
                            }else {
                                String ss[] = s.split(",");


                                Map<String,String> vs = new HashMap<>();
                                for(String ls:ss){
                                    if(ls.matches("@\\w+")){
                                        String attributeKey = ls.replace("@","");
                                        String attributeValue = element.attr(attributeKey);
                                        vs.put(attributeKey,attributeValue);
                                    }else{
                                        String text = element.text();//获取出标签的内容
                                        vs.put("text",text);
                                    }
                                }



                                ms.add(vs);
                            }
                        }
                    }
                    map.put(vName.getString(i),ms);
                }
            }
        }
    }

    public static Map<String,List<String>> output(Map<String,Object> map,JSONArray oName,JSONArray oValue,JSONArray oColumn){
        Map<String,Object> mapCopy= map;
        List<Map<String,Object>> list = new ArrayList<>();
        Object  obj = new Object();
        /*oName   href,text oValue  $content/#loop-$href,$content/#loop-$text*/
        boolean flat = true;
        Map<String, List<String>> nmap = new HashMap<>();
        for (int i = 0; i < oName.size(); i++) {//2
            List<String> nlist = new ArrayList<>();
            String joName = (String) oName.getString(i);//text
            String joValue = (String) oValue.getString(i);//$content/#loop-$text,$href
            String jovs[] = joValue.split("/");
            for (String jov : jovs) {
                if (jov.contains("$") && !jov.contains("#loop")) {
                    obj = mapCopy.get(jov.replace("$", ""));
                    //nlist.add(obj.toString());
                } else if (jov.contains("#loop")) {
                   /* if(flat){
                        nlist = new ArrayList<>();
                        flat = false;
                    }*/
                    list = (ArrayList) obj;
                    String strs[] = jov.split("-");
                    if (strs.length > 1) {
                        String key = strs[1].replace("$", "");
                        for(Map<String,Object> m:list) {//25
                            String value = m.get(key) + "";
                            nlist.add(value);
                        }
                    }
                }
            }
            nmap.put(joName,nlist);
        }
        return nmap;
    }






}
